﻿using System.Collections.Generic;
using Entitas;
using System.Linq;
class EnemyPursueTargetSystem : ReactiveSystem<CommandEntity>, ICleanupSystem
{
    EnemyContext enemyContext;
    PlayerContext playerContext;
    CommandContext commandContext;
    public EnemyPursueTargetSystem(Contexts contexts) : base(contexts.command)
    {
        enemyContext = contexts.enemy;
        playerContext = contexts.player;
        commandContext = contexts.command;
    }
    protected override void Execute(List<CommandEntity> entities)
    {
        foreach (var e in entities)
        {
            var enemy = enemyContext.GetEntityWithName(e.battleMovementCommand.Owner);
            var target = playerContext.GetEntityWithName(enemy.targetInformation.Name);
            var attackpos = new EntitasVec3();
            var newposition = enemy.position.value;

            if (enemy.position.value.x > target.position.value.x)
                attackpos.x = target.position.value.x + 2f;
            else attackpos.x = target.position.value.x - 2f;
            attackpos.y = target.position.value.y;

            if (enemy.position.value.x > target.position.value.x)
                enemy.ReplaceDirection(Direction.Left);
            else enemy.ReplaceDirection(Direction.Right);


            switch (e.battleMovementCommand.direction)
            {
                case Direction.Up:
                    newposition.y += enemy.speed.value;
                    newposition.y = EntitasVec3.ClampMaxValue(newposition.y, attackpos.y);
                    enemy.ReplacePosition(newposition);
                    break;
                case Direction.Up_Left:
                    newposition.x -= enemy.speed.value;
                    newposition.x = EntitasVec3.ClampMinValue(newposition.x, attackpos.x);
                    newposition.y += enemy.speed.value;
                    newposition.y = EntitasVec3.ClampMaxValue(newposition.y, attackpos.y);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the Up_Left");
                    break;
                case Direction.Up_Right:
                    newposition.x += enemy.speed.value;
                    newposition.x = EntitasVec3.ClampMaxValue(newposition.x, attackpos.x);
                    newposition.y += enemy.speed.value;
                    newposition.y = EntitasVec3.ClampMaxValue(newposition.y, attackpos.y);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the Up_Right");
                    break;
                case Direction.Down:
                    newposition.y -= enemy.speed.value;
                    newposition.y = EntitasVec3.ClampMinValue(newposition.y, attackpos.y);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the down");
                    break;
                case Direction.Down_Left:
                    newposition.x -= enemy.speed.value;
                    newposition.x = EntitasVec3.ClampMinValue(newposition.x, attackpos.x);
                    newposition.y -= enemy.speed.value;
                    newposition.y = EntitasVec3.ClampMinValue(newposition.y, attackpos.y);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the Down_Left");
                    break;
                case Direction.Down_Right:
                    newposition.x += enemy.speed.value;
                    newposition.x = EntitasVec3.ClampMaxValue(newposition.x, attackpos.x);
                    newposition.y -= enemy.speed.value;
                    newposition.y = EntitasVec3.ClampMinValue(newposition.y, attackpos.y);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the Down_Right");
                    break;
                case Direction.Left:
                    newposition.x -= enemy.speed.value;
                    newposition.x = EntitasVec3.ClampMinValue(newposition.x, attackpos.x);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the left");
                    break;
                case Direction.Right:
                    newposition.x += enemy.speed.value;
                    newposition.x = EntitasVec3.ClampMaxValue(newposition.x, attackpos.x);
                    enemy.ReplacePosition(newposition);
                    ServiceManager.DebugMessageService.Print("moving to the Right");
                    break;
                default:
                    break;
            }
        }
    }

    protected override bool Filter(CommandEntity entity)
    {
        return entity.hasBattleMovementCommand;
    }

    protected override ICollector<CommandEntity> GetTrigger(IContext<CommandEntity> context)
    {
        return context.CreateCollector(CommandMatcher.BattleMovementCommand.Added());
    }

    public void Cleanup()
    {
        foreach (var e in commandContext.GetEntities().Where(x => x.hasBattleMovementCommand == true))
        {
            e.Destroy();
        }
    }

}
